package com.buba.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.buba.springcloud.util.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {

    private RestTemplate restTemplate;

    @GetMapping(value = "/order/{id}")
    @SentinelResource(blockHandlerClass = ExceptionUtils.class, blockHandler = "handleBlock", fallbackClass = ExceptionUtils.class, fallback = "handleFallback")
    public String order(@PathVariable(value = "id") Integer id) {
        if (4 == id) {
            throw new IllegalArgumentException("非法参数异常");
        }
        return this.restTemplate.getForObject("http://provider" + "/provider/get/" + id, String.class);
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

   /* @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    private RestTemplate restTemplate;

    //请求的服务名称
    private String serverURL = "http://provider";

    @GetMapping("/consumer/get/{id}")
    public String paymentInfo(@PathVariable("id") Long id) {
        return restTemplate.getForObject(serverURL + "/provider/get/" + id, String.class);
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }*/
}